import { Component, OnInit, ViewChild } from '@angular/core';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';
import { _HttpClient } from '@delon/theme';
import { SFSchema, SFUISchema } from '@delon/form';
import { environment } from '@env/environment';
import { CacheService } from '@delon/cache';

@Component({
  selector: 'app-simple-codegen-edit',
  templateUrl: './edit.component.html',
})
export class SimpleCodegenEditComponent implements OnInit {
  i: any = this.cache.get('/codegen/info')
  schema: SFSchema = {
    properties: {
      model: { type: 'string', title: '模块' },
      author: { type: 'string', title: '作者'},
      rootPath: { type: 'string', title: '根路径'},
      targets: { type: 'string', title: '对象', enum: ['sql', 'entity', 'dao', 'dto', 'service', 'controller', 'list', 'edit'],},
    },
    required: ['model', 'author', 'rootPath', 'description', 'targets'],
  };
  ui: SFUISchema = {
    '*': {
      spanLabelFixed: 100,
      grid: { span: 24 },
    },
    $model: {
      widget: 'string'
    },
    $author: {
      widget: 'string'
    },
    $rootPath: {
      widget: 'string'
    },
    $targets: {
      widget: 'checkbox',
      checkAll: true,
    },
  };

  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public http: _HttpClient,
    public cache: CacheService
  ) {}

  ngOnInit(): void {
  }

  save(value: any) {
    this.cache.set('/codegen/info', value)
    this.http.post(`${environment.simple.ctx}/codegen/generate`, value).subscribe(res => {
      this.msgSrv.success('生成成功');
      this.modal.close(true);
    });
  }

  close() {
    this.modal.destroy();
  }
}
